package com.longteng.lesson2.httpinterface;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.List;

public class InterfaceTestForHttp {

    @Test
    public void getTest() {
//        String url ="http://115.28.108.130:8080/DemoController/getUserById?id=1";
        String url = "http://115.28.108.130:8080/DemoController/getUserById";
        //1.创建httpclient
        //2.创建一个get方法
        //执行拿到返回信息，打印结果
        HttpClient httpClient = HttpClients.createDefault();

        try {
            URIBuilder uriBuilder = new URIBuilder(url);
            uriBuilder.addParameter("id", "1");
            HttpGet httpGet = new HttpGet(uriBuilder.build());
            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            int code = httpResponse.getStatusLine().getStatusCode();
            System.err.println(code);
            String result = EntityUtils.toString(httpEntity);
            System.err.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
//        HttpPost HttpGet

    }

    @Test
    public void postTest() {
        String url = "http://115.28.108.130:8080/DemoController/createUser";
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        try {
            httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
            String s = "{\"username\":\"longteng\",\"age\":20}";
            StringEntity stringEntity = new StringEntity(s);
            httpPost.setEntity(stringEntity);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            int code = httpResponse.getStatusLine().getStatusCode();
            Assert.assertEquals(200, code);
            System.out.println(httpResponse.getStatusLine().getStatusCode());
            System.out.println(EntityUtils.toString(httpResponse.getEntity()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void Test415() {
        HttpClient httpClient = HttpClients.createDefault();
        String url = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
        HttpPost httpPost = new HttpPost(url);
        try {
            httpPost.addHeader(HTTP.CONTENT_TYPE, "text/xml;charset=utf-8");
            StringEntity stringEntity = new StringEntity("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"\n" +
                    "xmlns:web=\"http://WebXml.com.cn/\">\n" +
                    " <soap:Header/>\n" +
                    " <soap:Body>\n" +
                    " <web:getSupportCity>\n" +
                    " <!--Optional:-->\n" +
                    " <web:byProvinceName>辽宁</web:byProvinceName>\n" +
                    " </web:getSupportCity>\n" +
                    " </soap:Body>\n" +
                    "</soap:Envelope>\n");
            httpPost.setEntity(stringEntity);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            System.out.println(httpResponse.getStatusLine().getStatusCode());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void sessionTest() throws Exception {
        HttpClient httpClient = HttpClients.createDefault();
//        String loginUrl = "http://127.0.0.1:8888/login/login";
//        URIBuilder uriBuilder =new URIBuilder(loginUrl);
//        uriBuilder.setParameter("password","abc");
//        uriBuilder.setParameter("userName","absdfs");
//        HttpPost httpPost=new HttpPost(uriBuilder.build());
//        HttpClientContext context=new HttpClientContext();
//        httpClient.execute(httpPost,context);
//        List<Cookie> cookieList=context.getCookieStore().getCookies();
//        cookieList.forEach(cookie -> {
//            System.out.println(cookie.getName());
//            System.out.println(cookie.getValue());
//        });

        HttpClientContext context=new HttpClientContext();
        CookieStore cookieStore=new BasicCookieStore();
        Cookie cookie =new BasicClientCookie("JSESSIONID","E17561CC0F1842C99E050D2FE317F139");
        ((BasicClientCookie) cookie).setDomain("127.0.0.1");
        ((BasicClientCookie) cookie).setPath("/");
        cookieStore.addCookie(cookie);
        context.setCookieStore(cookieStore);

        String url = "http://127.0.0.1:8888/rest/hi";
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet,context);
            System.out.println(httpResponse.getStatusLine().getStatusCode());
            String body = EntityUtils.toString(httpResponse.getEntity());
            System.out.println(body);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
